package com.gurid.service.edu.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gurid.common.base.result.R;
import com.gurid.service.edu.entity.Course;
import com.gurid.service.edu.entity.form.CourseInfoForm;
import com.gurid.service.edu.entity.vo.CoursePublishVo;
import com.gurid.service.edu.entity.vo.CourseQueryVo;
import com.gurid.service.edu.entity.vo.CourseVo;
import com.gurid.service.edu.service.CourseService;
import com.gurid.service.edu.service.VideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Gurid
 * @since 2024-04-29
 */
@Tag(name = "CourseController", description = "课程管理")
//@CrossOrigin //允许跨域
@RestController
@RequestMapping("/admin/edu/course")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private VideoService videoService;

    @Operation(summary = "新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(
            @Parameter(description = "课程基本信息", required = true)
            @RequestBody CourseInfoForm infoForm) {
        String courseId = courseService.saveCourseInfo(infoForm);
        return R.ok().data("courseId", courseId).message("保存成功");
    }

    @Operation(summary = "查询课程", description = "根据id查询课程信息")
    @GetMapping("course-info/{id}")
    public R getById(
            @Parameter(description = "课程Id", required = true)
            @PathVariable String id) {
        CourseInfoForm infoForm = courseService.getCourseInfoById(id);
        if (infoForm != null){
            return R.ok().data("item", infoForm).message("查询成功");
        } else {
            return R.ok().message("数据不存在");
        }
    }

    @Operation(summary = "更新课程", description = "根据id更新课程信息")
    @PutMapping("update-course-info")
    public R updateCourseInfo(
            @Parameter(description = "课程Id", required = true)
            @RequestBody CourseInfoForm infoForm) {
        courseService.updateCourseInfoById(infoForm);
        return R.ok().message("更新成功");
    }

    @Operation(summary = "课程分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@Parameter(description = "当前页码", required = true) @PathVariable Long page,
                      @Parameter(description = "每页的记录数", required = true) @PathVariable Long limit,
                      @Parameter(description = "课程列表查询对象") CourseQueryVo courseQueryVo){
        IPage<CourseVo> pageModel = courseService.selectPage(page, limit, courseQueryVo);
        List<CourseVo> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @Operation(summary = "删除课程", description = "根据id删除课程")
    @DeleteMapping("remove/{id}")
    public R removeById(@Parameter(description = "课程id", required = true) @PathVariable String id) {

        //删除课程封面
        courseService.removeCoverById(id);
        //删除课程视频
        videoService.removeMediaVideoByCourseId(id);
        //删除课程
        boolean result = courseService.removeCourseById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @Operation(summary = "查询发布信息", description = "根据id获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R getCoursePublishVoById(@Parameter(description = "课程id", required = true) @PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item", coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @Operation(summary = "发布课程", description = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(@Parameter(description = "课程id", required = true) @PathVariable String id) {
        boolean result = courseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("发布失败");
        }
    }
}
